package com.zhongan.gateway.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import com.zhongan.gateway.common.constant.GwDeveloperApiConstant;
import com.zhongan.gateway.service.RedisService;

@Component
public class RedisServiceImpl implements RedisService {

    private static final Logger logger               = LoggerFactory.getLogger(RedisServiceImpl.class);
    @Autowired
    private JedisPool           jedisPool;

    private final static int    OVERFLOW_EXPIRE_TIME = 1;

    private final static int    FUSE_COUNT_TIME      = 60;

    private final static int    FUSE_CHECK_TIME      = 200;

    private final static int    FUSE_MAX_COUNT       = 3;

    @Override
    public Jedis getJedis() {

        return jedisPool.getResource();
    }

    @Override
    public String get(Jedis jedis, String key) {

        return jedis.get(key);

    }

    /*
     * (non-Javadoc)
     * @see 流量限制
     */
    @Override
    public boolean overFlow(String orgkey, int max) {
        Jedis jedis = null;
        String key = GwDeveloperApiConstant.GW_OVERFLOW + orgkey;
        try {
            jedis = jedisPool.getResource();
            boolean isExsit = jedis.exists(key);
            if (!isExsit) {
                synchronized (this) {
                    if (!jedis.exists(key)) {
                        jedis.incr(key);
                        jedis.expire(key, OVERFLOW_EXPIRE_TIME);
                        return true;
                    }
                }

            }

            long i = jedis.incr(key);
            if (i > max) {

                return false;
            }

        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        } finally {
            closeJedis(jedis);
        }
        return true;
    }

    @Override
    public int fuse(String key) {
        int count = 0;
        Jedis jedis = null;
        //熔断计数
        String countKey = GwDeveloperApiConstant.GW_FUSE_COUNT + key;
        //是否已熔断
        String checkKey = GwDeveloperApiConstant.GW_FUSE_CHECK + key;
        try {

            jedis = jedisPool.getResource();
            //是否已经熔断
            boolean isExsit = jedis.exists(checkKey);
            if (!isExsit) {
                boolean isCountExsit = jedis.exists(countKey);
                //count是否已存在
                if (!isCountExsit) {
                    synchronized (this) {
                        if (!jedis.exists(countKey)) {
                            Long i = jedis.incr(countKey);
                            jedis.expire(countKey, FUSE_COUNT_TIME);
                            return i.intValue();
                        }
                    }
                }
                Long i = jedis.incr(countKey);
                count = i.intValue();
                if (i > FUSE_MAX_COUNT) {
                    jedis.setex(checkKey, FUSE_CHECK_TIME, "1");
                }

            }

        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        } finally {
            closeJedis(jedis);
        }
        return count;
    }

    @Override
    public boolean isFused(String key) {
        Jedis jedis = null;
        boolean isFused = false;

        String checkKey = GwDeveloperApiConstant.GW_FUSE_CHECK + key;
        try {

            jedis = jedisPool.getResource();
            //是否已经熔断
            if (jedis.exists(checkKey)) {
                isFused = true;
            }

        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        } finally {
            closeJedis(jedis);
        }
        return isFused;
    }

    @Override
    public void closeJedis(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }

    }

    @Override
    public void openFused(String key) {
        Jedis jedis = null;
        //熔断计数
        String countKey = GwDeveloperApiConstant.GW_FUSE_COUNT + key;
        //是否已熔断
        String checkKey = GwDeveloperApiConstant.GW_FUSE_CHECK + key;
        try {

            jedis = jedisPool.getResource();
            jedis.del(countKey, checkKey);

        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        } finally {
            closeJedis(jedis);
        }

    }

    @Override
    public void clearCache() {
        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();
            jedis.flushDB();

        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        } finally {
            closeJedis(jedis);
        }

    }

    @Override
    public void clearCacheByKey(String key) {
        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();
            jedis.del(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            closeJedis(jedis);
        }

    }

    @Override
    public void set(Jedis jedis, String key, String value) {
        jedis.set(key, value);
    }
}
